package com.cby.library.tool

import android.app.Application
import android.content.Context
import android.content.Intent
import android.widget.ImageView
import com.cby.library.tool.crash.test.CrashTestActivity
import com.cby.library.tool.crash.ui.CrashActivity
import com.cby.library.tool.crash.ui.CrashDetailsActivity
import com.cby.library.tool.crash.ui.CrashInfoListActivity
import com.cby.library.tool.network.ui.NetRequestActivity
import com.cby.library.tool.network.ui.NetworkDetailActivity
import com.cby.library.tool.network.utils.NetworkTool
import com.cby.library.tool.ui.CbyToolsActivity
import com.cby.library.tool.ui.ModeHelper
import com.cby.library.tool.util.LogUtils
import com.cby.library.tool.widget.floating.FloatWindow
import com.cby.library.tool.widget.floating.PermissionListener
import com.cby.library.tool.widget.floating.Screen
import com.cby.library.tool.widget.floating.ViewStateListenerAdapter
import java.lang.Exception

object CbyTools {

    private var app: Application? = null

    fun init(application: Application?) {
        app = application
        NetworkTool.getInstance().init(application)
    }


    @JvmStatic
    fun getApplication(): Application? {
        return app
    }

    /**
     * 设置全局悬浮按钮，点击可以去网络列表页面
     * @param context                           上下文
     */
    fun setFloat(context: Context) {
        val imageView = ImageView(context.applicationContext)
        imageView.setImageResource(R.drawable.ic_show_error)
        FloatWindow
            .with(context)
            .setView(imageView) //设置悬浮控件宽高
            .setWidth(Screen.width, 0.2f)
            .setHeight(Screen.width, 0.2f) //设置控件初始位置
            .setX(Screen.width, 0.8f)
            .setY(Screen.height, 0.3f) //桌面显示
            .setDesktopShow(false) //设置这两个页面隐藏
            .setFilter(
                false, CbyToolsActivity::class.java,
                CrashActivity::class.java,
                CrashInfoListActivity::class.java,
                CrashDetailsActivity::class.java,
                CrashTestActivity::class.java,
                NetRequestActivity::class.java,
                NetworkDetailActivity::class.java
            ) //监听悬浮控件状态改变
            .setViewStateListener(ViewStateListenerAdapter())
            .setPermissionListener(object : PermissionListener {
                override fun onSuccess() {
                    LogUtils.i("NetworkTool-------PermissionListener------onSuccess")
                }

                override fun onFail() {
                    LogUtils.i("NetworkTool-------PermissionListener------onFail")
                }
            }) //监听权限申请结果
            .build()
        imageView.setOnClickListener { start(context.applicationContext) }
    }


    private fun start(context: Context) {
        try {
            val intent = Intent(context, CbyToolsActivity::class.java)
            //为activity开启新的栈，Intent.FLAG_ACTIVITY_NEW_TASK 设置状态，
            //首先查找是否存在和被启动的Activity具有相同的任务栈，如果有则直接把这个栈整体移到前台，并保持栈中的状态不变，
            //既栈中的activity顺序不变，如果没有，则新建一个栈来存放被启动的Activity。
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @JvmStatic
    fun isPreviewMode() : Boolean {
        return false
    }

    @JvmStatic
    fun isTest() : Boolean {
        return ModeHelper.getInstance(app).mode == 0
    }
}